package net.zhengxinyun.performance.service.examNew;

import com.sirdc.modules.utils.ObjectUtils;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.entity.exam.InquireInfoEntity;
import net.zhengxinyun.performance.entity.exam.InquireObjEntity;
import net.zhengxinyun.performance.entity.exam.UnSysUsers;
import net.zhengxinyun.performance.filter.UserFilter;
import net.zhengxinyun.performance.filter.exam.InquireObjFilter;
import net.zhengxinyun.performance.filter.exam.UnSysUserFilter;
import net.zhengxinyun.performance.mapper.examNew.InquireInfoEntityMapper;
import net.zhengxinyun.performance.mapper.examNew.InquireObjEntityMapper;
import net.zhengxinyun.performance.mapper.examNew.UnSysUsersMapper;
import net.zhengxinyun.performance.mapper.login.UserEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;

/**
 * Date: 2020/3/12 15:45
 * 调查对象总表和调查对象信息表操作；
 */
@Service
public class InquireObjServiceImpl implements InquireObjService {

    @Autowired
    private InquireInfoEntityMapper infoMapper;
    @Autowired
    private InquireObjEntityMapper objMapper;

    @Autowired
    private UserEntityMapper userEntityMapper;

    @Autowired
    private UnSysUsersMapper unSysUsersMapper;

    @Override
    public int deleteInfoByPrimaryKey(Long id) {
        return infoMapper.deleteByPrimaryKey(id);
    }

    public int insertInfo(List<InquireInfoEntity> list) {
        int i = 0;
        for (InquireInfoEntity item : list) {
            item.setId(null);
            int t = infoMapper.insert(item);
            i = i + t;
        }

        return i;
    }

    @Override
    public int insertInfoInfoSelective(InquireInfoEntity record) {
        return infoMapper.insertSelective(record);
    }

    @Override
    public InquireInfoEntity selectInfoByPrimaryKey(Long id) {
        return infoMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateInfoByPrimaryKeySelective(InquireInfoEntity record) {
        return infoMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateInfoByPrimaryKey(InquireInfoEntity record) {
        return infoMapper.updateByPrimaryKey(record);
    }

    /**----
     * 调查对象总表
     */

    /**
     * 调查对象
     *
     * @param id
     * @return
     */
    @Override
    public int deleteObjByPrimaryKey(Long id) {
        return objMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertObj(InquireObjEntity record) {
        return objMapper.insert(record);
    }

    @Override
    public int insertObjSelective(InquireObjEntity record) {
        return objMapper.insertSelective(record);
    }

    @Override
    public InquireObjEntity selectObjByPrimaryKey(Long id) {
        return objMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateObjByPrimaryKeySelective(InquireObjEntity record) {
        return objMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateObjByPrimaryKey(InquireObjEntity record) {
        return objMapper.updateByPrimaryKey(record);
    }

    @Override
    public List<InquireObjEntity> selectListObjs(InquireObjFilter filter) {
        return objMapper.selectListObjs(filter);
    }

    @Override
    public List<InquireObjFilter> selectListInfos(InquireObjFilter entity) {
        return infoMapper.selectListInfos(entity);
    }

    /**
     * 抽选规则
     *
     * @return
     */
    @Override
    public List<UnSysUserFilter> chooseRule(String ruleType, int ruleNum, String userType, String userSource, int totalNum) {
        HashMap<String, Object> data = new HashMap<>();
        List<UnSysUserFilter> unSysUsers = new ArrayList<>();
        if (userSource.equals("系统")) {
            List<UserFilter> userFilters = userEntityMapper.queryAllUsers(new UserFilter());
            for (UserFilter item : userFilters) {
                UnSysUserFilter user = new UnSysUserFilter();
                user.setId(item.getId());
                user.setUserName(item.getUserName());
                user.setUserTel(item.getUserPhone());
                user.setUserType("");
                unSysUsers.add(user);
            }
        } else if ("非系统".equals(userSource)) {

            UnSysUsers filter = new UnSysUsers();
            List<String> types = Util.strToList(userType);
            //有过滤条件 选用过滤条件
            for (String type : types) {
                filter.setUserType(type);
                List<UnSysUserFilter> unSysUserFilters = unSysUsersMapper.seletListUsers(filter);
                unSysUsers.addAll(unSysUserFilters);
            }
            //无过滤条件 直接选择全部
            if (ObjectUtils.isBlank(types)) {
                unSysUsers = unSysUsersMapper.seletListUsers(filter);
            }

        } else {
            System.out.println("错误");
        }
        List<UnSysUserFilter> ruleUser = new ArrayList<>();
        switch (ruleType) {
            case "随机规则":

                ruleUser = randomRule(unSysUsers, totalNum);
                break;
            case "特定规则":
                ruleNum = ruleNum + 1;
                for (int i = 0; i < unSysUsers.size(); i++) {
                    if ((i + 1) % ruleNum == 0) { // 每隔几个选一个；
                        UnSysUserFilter user = new UnSysUserFilter();
                        user.setId(unSysUsers.get(i).getId());
                        user.setUserName(unSysUsers.get(i).getUserName());
                        user.setUserType(unSysUsers.get(i).getUserType());
                        ruleUser.add(user);
                        if (totalNum != 0 && ruleUser.size() == totalNum) {
                            break;

                        }
                    }
                }

                break;
            default:
                return null;
        }


        return ruleUser;
    }

    List<UnSysUserFilter> randomRule(List<UnSysUserFilter> ruleUser, int count) {
        List<UnSysUserFilter> newlist = new ArrayList<>();
        Random random = new Random();
        List<Integer> tempList = new ArrayList<Integer>();
        if(ruleUser.size()<count){
            return ruleUser;
        }

        for (int i = 0; i < count; i++) {
            int i1 = random.nextInt(ruleUser.size());
            if (!tempList.contains(i1)) {
                tempList.add(i1);
                newlist.add(ruleUser.get(i1));
            } else {
                i--;
            }
        }
        return newlist;
    }


}


